using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Data;

using TSFileOutput.Data;

namespace TSFileOutput.Core
{
    public abstract class DataOutputManagerBase : IDisposable 
    {
        public FileDataColumnCollection DataColumns = new FileDataColumnCollection();

        public abstract string ErrMsg
        {
            get;
            set;
        }

        public DataOutputManagerBase()
        {

        }

        public abstract ArrayList GenerateFileContent(string ProjectManager);

        #region FileDataColumnBase

        public class FileDataColumn
        {
            public int ColumnIndex = 0;
            public int Size = 0;
            public string Name = "";
            public string ActualName = "";
            public bool FromBack = false;

            public FileDataColumn(int index, int size, string name)
            {
                this.ColumnIndex = index;
                this.Size = size;
                this.Name = name;
            }

            public FileDataColumn(int index, int size, string name, string actual, bool back)
            {
                this.ColumnIndex = index;
                this.Size = size;
                this.Name = name;
                this.ActualName = actual;
                this.FromBack = back;
            }
        }

        public class FileDataColumnCollection : CollectionBase
        {
            public FileDataColumn this[int index]
            {
                get { return ((FileDataColumn)List[index]); }
                set { List[index] = value; }
            }

            public int Add(FileDataColumn value)
            {
                return (List.Add(value));
            }

            public int IndexOf(FileDataColumn value)
            {
                return (List.IndexOf(value));
            }

            public void Insert(int index, FileDataColumn value)
            {
                List.Insert(index, value);
            }

            public void Remove(FileDataColumn value)
            {
                List.Remove(value);
            }

            public bool Contains(FileDataColumn value)
            {
                // If value is not of type Int16, this will return false.
                return (List.Contains(value));
            }
        }
        #endregion

        public void Dispose()
        {
            //Dispose();
        }
        
    }

    #region TimeEntryTransactionDataOutputManager
    public class TimeEntryTransactionDataOutputManager : DataOutputManagerBase
    {
        private string errmsg = string.Empty;

        public override string ErrMsg
        {
            get { return  errmsg; }
            set { errmsg = value; }
        }

        public TimeEntryTransactionDataOutputManager()
        {            
            // defined data column
            //this.DataColumns.Add(new FileDataColumn(0, 18, "ID", "ID", true));
            this.DataColumns.Add(new FileDataColumn(0, 50, "Consultant Name", "UserCode", false));
            this.DataColumns.Add(new FileDataColumn(1, 10, "Date", "Date", true));
            this.DataColumns.Add(new FileDataColumn(2, 6, "Hour", "Hour", true));
            this.DataColumns.Add(new FileDataColumn(3, 20, "Project Code", "PrjCode", true));
            this.DataColumns.Add(new FileDataColumn(4, 250, "Project Name", "PrjName", true));
            //this.DataColumns.Add(new FileDataColumn(5, 1, "Billable", "Billable", false));
            //this.DataColumns.Add(new FileDataColumn(6, 256, "Description", "Description", false));
            //this.DataColumns.Add(new FileDataColumn(8, 1, "Status", "Status", false));
            //this.DataColumns.Add(new FileDataColumn(9, 100, "SAP B1 Database", "SAPB1DB", true));
            this.DataColumns.Add(new FileDataColumn(5, 100, "Task", "Task", true));
            this.DataColumns.Add(new FileDataColumn(6, 100, "Comments", "Comments", true));
        }        

        #region GenerateFileContent
        public override ArrayList GenerateFileContent(string PM)
        {
            ArrayList ResultList = null;
            int counter = 0;

            //if (this.TryConnection())
            {
                DataSet ds = SqlHelper.ExecuteDataSet(TSFileOutput.Data.Data.ConnectionString, CommandType.StoredProcedure, "sp_TimeMyEntryReport_Email",
                    TSFileOutput.Data.Data.CreateParameter("@IN_UserCode", PM));

                DataTable ResultTable = ds.Tables[0];

                if (ResultTable != null)
                {
                    // generate file now
                    ResultList = new ArrayList();
                    ResultList.Add(new string[] { "Consultant Name", "Date", "Hour", "Project Code", "Project Name", "Task", "Comments" });
                    try
                    {
                        if (ResultTable.Rows.Count > 0)
                        {
                            TSConfigurator.FromDate = ResultTable.Rows[0]["FromDate"].ToString();
                            TSConfigurator.ToDate = ResultTable.Rows[0]["ToDate"].ToString();
                        }
                        foreach (DataRow ResultRow in ResultTable.Rows)
                        {
                            //string[] ResultArray = new string[this.DataColumns.Count];                            
                            string[] ResultArray = new string[7];                            
                            ResultArray[this.DataColumns[0].ColumnIndex] = ResultRow[this.DataColumns[0].ActualName].ToString();
                            ResultArray[this.DataColumns[1].ColumnIndex] = ResultRow[this.DataColumns[1].ActualName].ToString();
                            ResultArray[this.DataColumns[2].ColumnIndex] = ResultRow[this.DataColumns[2].ActualName].ToString();
                            ResultArray[this.DataColumns[3].ColumnIndex] = ResultRow[this.DataColumns[3].ActualName].ToString();
                            ResultArray[this.DataColumns[4].ColumnIndex] = ResultRow[this.DataColumns[4].ActualName].ToString().Replace(",", "-");
                            ResultArray[this.DataColumns[5].ColumnIndex] = ResultRow[this.DataColumns[5].ActualName].ToString().Replace(",", "-");
                            ResultArray[this.DataColumns[6].ColumnIndex] = ResultRow[this.DataColumns[6].ActualName].ToString().Replace(",", "-");
                            //ResultArray[this.DataColumns[7].ColumnIndex] = ResultRow[this.DataColumns[7].ActualName].ToString();
                            //ResultArray[this.DataColumns[8].ColumnIndex] = ResultRow[this.DataColumns[8].ActualName].ToString();
                            //ResultArray[this.DataColumns[9].ColumnIndex] = ResultRow[this.DataColumns[9].ActualName].ToString();
                            //ResultArray[this.DataColumns[10].ColumnIndex] = ResultRow[this.DataColumns[10].ActualName].ToString();
                            //ResultArray[this.DataColumns[11].ColumnIndex] = ResultRow[this.DataColumns[11].ActualName].ToString();
                            ResultList.Add(ResultArray);
                            counter++;
                        }

                    }
                    catch (Exception ex)
                    {
                        this.ErrMsg = ex.Message;
                    }
                }
                else
                {
                    this.ErrMsg = "[TimeEntryTransactionDataOutputManager.GenerateFileContent] : this.SqlHelper.ExecuteDataSet('sp_TimeMyEntryReport', '01/01/2013', '" + DateTime.Now.ToString("MM/dd/yyyy") + "' , '" + PM + "') : " + SqlHelper.ErrMsg;
                }
            }

            return ResultList;
        }
        #endregion

    }
    #endregion

}
